/*
 * Copyright (c) www.bugull.com
 */

package com.bugull.farm.device.balancer.handler;

import org.apache.logging.log4j.*;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

/**
 *
 * @author Frank Wen(xbwen@hotmail.com)
 */
public class DeviceChannelStatusHandler extends ChannelHandlerAdapter {
    
    private final static Logger logger = LogManager.getLogger(DeviceChannelStatusHandler.class);
    
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        logger.error("channel closed because an exception is caught", cause);
        Channel ch = ctx.channel();
        if (ch.isActive()) {
            ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
        }
    }

}
